package ui.activity.user;

import java.io.IOException;
import java.sql.SQLException;

import ui.model.user.UserLoginViewModel;
import util.web.WebServiceHelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import business.entity.User;
import business.service.user.CardSyncService;
import business.service.user.IUserLoginService;
import business.service.user.impl.UserLoginService;

import com.lab107team.R;


/**
 * @author Y_x
 * 
 */
@SuppressLint("HandlerLeak")
public class LoginActivity extends Activity {
	private static final String TAG = LoginActivity.class.getSimpleName();

	private static final int LOGIN_OK = 1;
	private static final int LOGIN_NOT_OK = 0;

	private ImageButton btnGoBack = null;
	private Button btnLogin = null;
	private Button btnRegist = null;

	private EditText etAccount = null;
	private EditText etPassword = null;

	private ProgressDialog pdWait = null;

	private IUserLoginService service = null;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			System.out.println("okokok");
			pdWait.dismiss();
			if (msg.what == LOGIN_OK) {

				new Thread(new Runnable() {

					@Override
					public void run() {
						Log.d(TAG, "同步名片");
						String username = etAccount.getText().toString();
						String password = etPassword.getText().toString();
						try {
							(new CardSyncService()).update(username, password);

						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();

				finish();

			} else if (msg.what == LOGIN_NOT_OK) {
				String errMsg = (String) msg.obj;
				Toast.makeText(LoginActivity.this, errMsg, Toast.LENGTH_SHORT).show();
			}
		}
	};

	private UserLoginViewModel model;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity_layout);

		model = new UserLoginViewModel();
		service = new UserLoginService((UserLoginViewModel) model);
		
		initControlsAndRegEvent();
	}

	protected void initControlsAndRegEvent() {
		btnGoBack = (ImageButton) findViewById(R.id.btn_login_user_go_back);
		btnLogin = (Button) findViewById(R.id.btn_login_user_login);
		btnRegist = (Button) findViewById(R.id.btn_login_user_regist);
		etAccount = (EditText) findViewById(R.id.et_login_user_account);
		etPassword = (EditText) findViewById(R.id.et_login_user_password);

		btnGoBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btnRegist.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
		btnLogin.setOnClickListener(new LoginBtnClickListener());


		initProgressDialog();
	}

	private void initProgressDialog() {
		pdWait = new ProgressDialog(this);
		pdWait.setTitle("请稍候..");
		pdWait.setMessage("正在验证..");
	}

	private class LoginBtnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			final Message msg = new Message();
			msg.what = LOGIN_NOT_OK;
			if (!WebServiceHelper.isconnected(LoginActivity.this)) {
				msg.obj = "请检查网络";
				handler.sendMessage(msg);
				return;
			}
			String account = etAccount.getText().toString().trim();
			String password = etPassword.getText().toString().trim();
			final User user = new User(account, password);
			pdWait.show();
			
			new Thread(new Runnable() {

				@Override
				public void run() {
					boolean flag = service.login(user);
					if (flag) {
						msg.what = LOGIN_OK;
					} else {
						msg.what = LOGIN_NOT_OK;
						msg.obj = (Object) "登录失败";
					}
					handler.sendMessage(msg);
				}

			}).start();
		}
	}

}
